package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.LbsServiceRpcService;
import com.hunttown.mes.rpc.domain.LbsServiceDTO;
import com.hunttown.mes.rpc.domain.query.LbsServiceDTOQuery;
import com.hunttown.mes.service.LbsServiceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.LbsServiceConverts.convert;

/**
 * created by wangjunfu on 2021-07-05 11:06:19
 */
@Service
public class LbsServiceRpcServiceImpl implements LbsServiceRpcService {

    private final Logger logger = LoggerFactory.getLogger(LbsServiceRpcServiceImpl.class);

    private final LbsServiceService lbsServiceService;

    @Autowired
    public LbsServiceRpcServiceImpl(LbsServiceService lbsServiceService) {
        this.lbsServiceService = lbsServiceService;
    }

    @Override
    public LbsServiceDTO insert(LbsServiceDTO lbsServiceDTO) {
        try {
            return convert(lbsServiceService.insert(convert(lbsServiceDTO)));
        } catch (Exception e) {
            logger.error("LbsService create failed.", e);
            throw new RuntimeException("LbsService create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(LbsServiceDTO lbsServiceDTO) {
        try {
            return lbsServiceService.updateInfoById(convert(lbsServiceDTO));
        } catch (Exception e) {
            logger.error("LbsService update by id failed.", e);
            throw new RuntimeException("LbsService update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(LbsServiceDTOQuery lbsServiceDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsServiceDTOQuery.getW_id() != null && lbsServiceDTOQuery.getW_id() > 0) {
                query.put("w_id", lbsServiceDTOQuery.getW_id());
            }

            if (lbsServiceDTOQuery.getIds() != null && !lbsServiceDTOQuery.getIds().equals("")) {
                query.put("ids", lbsServiceDTOQuery.getIds());
            }

            if (lbsServiceDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", lbsServiceDTOQuery.getStopFlag());
            }

            if (lbsServiceDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsServiceDTOQuery.getDeleteFlag());
            }

            return lbsServiceService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("LbsService update by query failed.", e);
            throw new RuntimeException("LbsService update by query failed, Please order your code.", e);
        }
    }

    @Override
    public LbsServiceDTO selectById(Integer id) {
        try {
            return convert(lbsServiceService.selectById(id));
        } catch (Exception e) {
            logger.error("LbsService select by id failed.", e);
            throw new RuntimeException("LbsService select by id failed, Please order your code.", e);
        }
    }

    @Override
    public LbsServiceDTO selectObjByQuery(LbsServiceDTOQuery lbsServiceDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsServiceDTOQuery.getEntId() != null) {
                query.put("entId", lbsServiceDTOQuery.getEntId());
            }

            if (lbsServiceDTOQuery.getServiceCode() != null) {
                query.put("serviceCode", lbsServiceDTOQuery.getServiceCode());
            }

            if (lbsServiceDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", lbsServiceDTOQuery.getStopFlag());
            }

            if (lbsServiceDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsServiceDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(lbsServiceDTOQuery.getOrderBy())) {
                query.put("orderBy", lbsServiceDTOQuery.getOrderBy());
            }

            return convert(lbsServiceService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("LbsService select obj by query failed.", e);
            throw new RuntimeException("LbsService select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<LbsServiceDTO> getListForPage(LbsServiceDTOQuery lbsServiceDTOQuery) {
        Query query = Query.page(lbsServiceDTOQuery.getPn(), lbsServiceDTOQuery.getPageSize());

        if (lbsServiceDTOQuery.getId() != null && lbsServiceDTOQuery.getId() > 0) {
            query.put("id", lbsServiceDTOQuery.getId());
        }

        if (lbsServiceDTOQuery.getIds() != null && !lbsServiceDTOQuery.getIds().equals("")) {
            query.put("ids", lbsServiceDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(lbsServiceDTOQuery.getLikeName())) {
            query.put("likeName", lbsServiceDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(lbsServiceDTOQuery.getShowDate())) {
            query.put("showDate", lbsServiceDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(lbsServiceDTOQuery.getStartDate())) {
            query.put("startDate", lbsServiceDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(lbsServiceDTOQuery.getEndDate())) {
            query.put("endDate", lbsServiceDTOQuery.getEndDate());
        }

        if (lbsServiceDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", lbsServiceDTOQuery.getStopFlag());
        }

        if (lbsServiceDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", lbsServiceDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(lbsServiceDTOQuery.getOrderBy())) {
            query.put("orderBy", lbsServiceDTOQuery.getOrderBy());
        }

        List<LbsServiceDTO> list = convert(lbsServiceService.selectByQuery(query));
        Page<LbsServiceDTO> page = Page.from(lbsServiceDTOQuery.getPn(), lbsServiceDTOQuery.getPageSize(), list);
        page.setTotalItems(lbsServiceService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return lbsServiceService.deleteById(id);
        } catch (Exception e) {
            logger.error("LbsService delete failed.", e);
            throw new RuntimeException("LbsService delete failed, Please order your code.", e);
        }
    }
}